<?php

/*	Vulpes - Web-based managegent for wireless internet providers.
	Copyright (C) 2007 Alwin Garside

	This file is part of Vulpes.

	Vulpes is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	Vulpes is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>. */

switch ($page->action)
{
	case 'info':

		$client = new client(get('username'));
		$page->content .= $client->info();
		
		break;


	case 'insert':

		$client = new client();
		$client->username = get('new_username');
		$client->name = get('new_name');
		$client->ip = get('new_ip');
		$client->mac = get('new_mac');
		$client->status = get('new_status');
		$client->email = get('new_email');
		$client->password = hash('sha512', get('new_password'));
		$client->insert();
		iptables_refresh();
		
		break;

			
	case 'update':

		$client = new client(get('username'));
		if (get('new_name'))
			$client->name = get('new_name');
		if (get('new_ip'))
			$client->ip = get('new_ip');
		if (get('new_mac'))
			$client->mac = get('new_mac');
		if (get('new_status'))
			$client->status = get('new_status');
		if (get('new_email'))
			$client->email = get('new_email');
		$client->update();
		iptables_refresh();
						
		break;

			
	case 'delete':

		$sql_delete_client = '
			DELETE FROM `clients`
			WHERE `username` = "' .get('username'). '"';
		if (!query($sql_delete_client))
		{
			$page->error('{lang_client_not_deleted}');
			break;
		}
		else
		{
			$page->notify('{lang_client_deleted}');
		}

		if ($client->refresh())
			$page->notify('{lang_changes_applied}');
		else
			$page->error('{lang_changes_not_applied}');

}

$sql_clients = '
	SELECT `name`, `username`, `ip`, `mac`, `status`, `upload`, `download`, `email`
	FROM `clients` ';
$result_clients = query($sql_clients);

$page->content .= '<clients>';
while ($client = mysql_fetch_object($result_clients))
{
	$page->content .= '<client>';
	foreach ($client as $key => $value)
	{
		$page->content .= '<'.$key.'>' .$value. '</'.$key.'>';
	}
	$page->content .= '</client>';
}
$page->content .= '</clients>';

?>